<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Upload"
          v-hasPermi="['system:uploading:add']"
          @click="handleAdd"
          >上傳文件</el-button
        >
      </el-col>

    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      border
      :data="specificationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" label="序號" width="100" align="center" />
      <el-table-column
        label="文件分類"
        prop="fileType"
        align="center"
        :show-overflow-tooltip="true"
      >
       </el-table-column
      >
    
      <el-table-column label="文件名稱" align="center" prop="url">
  <template #default="scope">
    <el-link
      :href="scope.row.url"
      type="primary"
      :underline="false"
      target="_blank"
      class="file-link"
    >
      <el-icon><Document /></el-icon>
      {{ getFileName(scope.row.url) }}
    </el-link>
  </template>
</el-table-column>
      <el-table-column
        label="上傳时间"
        sortable
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
      >
      </el-table-column>
      <el-table-column label="創建人" align="center" prop="nickName"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
      
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:uploading:delete']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form
        ref="addclientele"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="文件类型:" prop="fileType">
          <el-select
                style="width: 97%"
                clearable
                v-model="form.fileType"
                placeholder="选择文件类型"
              >
                <el-option
                  v-for="item in contract_type "
                  :key="item.value"
                  :label="item.label"
                  :value="item.label"
                />
              </el-select>
        </el-form-item>

         <!-- 新增文件上傳 -->
    <el-form-item label="上傳文件:" prop="file">
      <file-upload v-model="form.url" />
    </el-form-item>

    
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
   
   <script setup name="uploading">
import {
  uploadContract,
  listContracts,
  delContract,
} from "@/api/business/uploading.js";

import { ref } from "vue";
import { getToken } from "@/utils/auth";
import { Document } from '@element-plus/icons-vue'

const router = useRouter();
const { proxy } = getCurrentInstance();

const { contract_type } = proxy.useDict("contract_type");


// 初始化格式化的选项数据

const specificationList = ref([]);
const open = ref(false);
const loading = ref(true);

const ids = ref([]);

const crmpictureIds = ref([]);

const total = ref(0);
const title = ref("");
const dateRange = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    fileType: [{ required: true, message: "文件类别不能为空", trigger: "blur" }],

    // size: [
    //   { required: true, message: "尺寸名称不能为空", trigger: "blur" },
    // ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listContracts(queryParams.value,dateRange.value).then(response => {
    specificationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(error => {
    loading.value = false;
  });
}

// 处理文件名的方法
const getFileName = (url) => {
  if (!url) return ''
  // 先获取最后一个斜杠后的内容
  const fullName = url.split('/').pop()
  // 去掉UUID部分（36个字符）
  return fullName.substring(36)
}



/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除为"' + id + '"的数据项?')
    .then(function () {
      return delContract(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}



/** 添加单头 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "上傳单头";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["addclientele"].validate((valid) => {
    if (valid) {
      // 合并数据
      const mergedData = { ...form.value};
      uploadContract(mergedData).then((response) => {
          proxy.$modal.msgSuccess("上传成功");
          open.value = false;
          getList();
        });
      
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = {
  
  };
  proxy.resetForm("addclientele");
}


getList();
</script>
  
  <style scoped>
.linkman-input-group {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.linkman-input-group .el-input {
  flex: 1;
  margin-right: 10px;
}
</style>